<?php
ob_start();
header("Content-type:text/html;charset=utf-8");
ini_set('memory_limit','88M');
ini_set('max_execution_time','0');
date_default_timezone_set('Asia/Shanghai');
error_reporting(0);
$date   = date('Y-m-d');//当天下午16:30之前的订单
$dateex = explode('-',$date);
$stime  = time();
$stime1 = mktime('2','10','00',$dateex[1],$dateex[2],$dateex[0]);
$stime2 = mktime('4','10','10',$dateex[1],$dateex[2],$dateex[0]);
if($stime>=$stime1 && $stime<=$stime2){
}else{
	echo "fuck you mom!!!get out(return money)!! \n";
	exit;
}

/*
*
*
*Author:xuanfeng_goudan
*Date: 2017-11-04 11:48
* crontab脚本：     * /3 1 * * * * php -f  /crontab_js.php 
*
*/
include 'config.php';

/*----------链接mysql数据库  start------------*/
$conn = @mysql_connect($db_host,$db_user,$db_pwd) or die('mysql server failed!!');
@mysql_query('set names utf8',$conn);
@mysql_select_db($db_name,$conn) or die('can not find mysql dbname:'.$db_name);
/*----------链接mysql数据库  end------------*/

/*----------常用方法封装  start------------*/
function querys($sql){
	return @mysql_query($sql);
}

function getone($sql){
	$query = querys($sql);
	return @mysql_fetch_assoc($query);
}

function getall($sql){
	$query = querys($sql);
	$list = '';
	while($row = @mysql_fetch_assoc($query)){
		$list[] = $row;
	}
	return $list;
}
$sys=getone("select * from clt_system where id=1");
$xingqi = date("w");
if($sys['jingtai']=='close'){
	echo "静态已关闭";
	exit;
}
$time=unserialize($sys['jingtai_ff']);
if(in_array($xingqi, $time) || in_array(8, $time)) {
}else {
	echo "当前日期不能发放";
	exit;
}
/*修改或者新增bonus表*/
function edit_add_bonus($uid,$base_mey_per){
	/*修改bonus表,先查一下今天有没有数据*/
	$td_time = strtotime(date('Y-m-d'));
	$sql_check_bouns = "select * from clt_bonus where uid='".$uid."' and `createtime`>='".$td_time."'";
	$sql_check_bouns = getone($sql_check_bouns);
	if(!empty($sql_check_bouns['id'])){
		//如果有数据，直接修改对应的列
		$sql_bonus = "update clt_bonus set `b2`=(b2+$base_mey_per),`b0`=(b0+$base_mey_per) where id='".$sql_check_bouns['id']."'";
	}else{
		$sql_bonus = "insert into clt_bonus (is_type,uid,b0,b2,createtime) values(0,'".$uid."',$base_mey_per,$base_mey_per,'".time()."');";
	}
	querys($sql_bonus);
}
/*把所有已处理过的数据设为已处理*/
function set_deal($uid){
	$sql = "update clt_users set `is_deal`=1 where id='".$uid."'";
	querys($sql);
}

/*获取当前会员当前钱包money_benjin的值*/
function getuser_money_benjin($id){
	$sql="select money_benjin from clt_users where id=".$id;
	$user=getone($sql);
	return $user['money_benjin'];
}
/*----------常用方法封装  end------------*/
$sql="select id,recount,relevel,rpath,money_benjin from clt_users where recount>1 and is_deal=2 and is_lock=0 and sign=0 order by id desc limit 200";
$users=getall($sql);//当前所有满足条件的会员
$level_sql="select * from clt_user_level where level=1";
$levels=getone($level_sql);/*等级信息，比例 */


foreach ($users as $k=>$v){
	//先读取所有未释放金额--start
	$licai_sum="select sum(summey) as allmey from clt_licai_order where uid=".$v['id'];
	$sum_summey=getone($licai_sum);
	$all_mey = $sum_summey['allmey'];
	//先读取所有未释放金额--end
	
	if($all_mey<=0 || empty($all_mey)){
		set_deal($v['id']);continue;
	}
	/*先找到这个人今天发了多少静态---start*/
	$today_time = strtotime(date('Y-m-d'));
	$sql_bonus = "select b1 from clt_bonus where uid='".$v['id']."' and createtime>='".$today_time."'";
	$bonus_ary = getone($sql_bonus);
	$bonus_that= $bonus_ary['b1'];
	
	if(empty($bonus_that)){
		set_deal($v['id']);continue;
	}
	/*先找到这个人今天发了多少静态---end*/
	
	//算出来这个人的推荐路径
	$new_path = $v['rpath'].$v['id'];
	//先判断这个人的rcount》2 并且 $all_mey]
	if($v['recount']>=2&&$all_mey>0){
		//先获取本人的比例  $real_lev
		$real_lev = $levels['b5']/100;
		$sql_pre_user = "select id,mobile from clt_users where up_userid='".$v['id']."' and activation=1";

		$pre_userlist = getall($sql_pre_user);
		foreach($pre_userlist as $ke=>$vo){
			//查这个下级会员的bonus表里面的b1值
			$pre_b1 = getone("select b1 from clt_bonus where uid='".$vo['id']."' and createtime>'".$today_time."'");

			$get_pre_b1 = $pre_b1['b1'];
			$mey=$get_pre_b1*$real_lev;
			//判断本人烧伤,下级会员的b1和本人b1,选择小的那个
			$bonus_shaoshang = $bonus_that<$mey?$bonus_that:$mey;
			//判断这条订单是否封顶,如果剩下的>要发的，取要发的，否则取剩下的
			$bonus_minu = ($all_mey-$bonus_shaoshang);
			$bonus_fengding  = $bonus_minu>0?$bonus_shaoshang:$all_mey;
			if($bonus_fengding>0){
				//给这个人发加速奖
				$beizhu = "获得第:1代会员" . $vo['mobile'] . "分享算力:" . $get_pre_b1 . " x " . $real_lev;
				/*给v会员加速 */
				$money_sql="update clt_users set money_benjin=money_benjin+".$bonus_fengding."  where id=".$v['id'];
				querys($money_sql);
				/*增加记录*/
				$money_benjin1=getuser_money_benjin($v['id']);
				$sql_money_log = "insert into clt_money_log values (NULL,'".time()."','+".$bonus_fengding."',3,'".$v['id']."','".$money_benjin1."','".$beizhu."',12,0,0,0)";
				querys($sql_money_log);
				edit_add_bonus($v['id'],$bonus_fengding);
				if ($all_mey - $bonus_fengding <= 0) {
					$all_mey = 0;
					break;
				} else {
					$all_mey = $all_mey - $bonus_fengding;
				}
			}
		}
	}


/*	//先判断这个人的rcount>=3 并且 $all_mey
	if($v['recount']<3 || empty($v['recount']) ||  empty($all_mey) || $all_mey<=0){
		//跳出本次循环
		set_deal($v['id']);continue;
	}*/
if($v['recount']>=3&&$all_mey>0){
	//先获取本人的比例2代  $real_lev
	$real_levl = $levels['b6']/100;
	$relevel1 = $v['relevel'] + 2;
	$sql_pre_user1 = "select id,mobile from clt_users where rpath like '%".$new_path."%' and relevel=".$relevel1."  and activation=1";
	$pre_userlist1 = getall($sql_pre_user1);
	foreach($pre_userlist1 as $ke=>$vo1){
		//查这个下级会员的bonus表里面的b1值
		$pre_b1 = getone("select b1 from clt_bonus where uid='".$vo1['id']."' and createtime>'".$today_time."'");
		$get_pre_b1 = $pre_b1['b1'];
		$mey=$get_pre_b1*$real_levl;
		//判断本人烧伤,下级会员的b1和本人b1,选择小的那个
		$bonus_shaoshang = $bonus_that<$mey?$bonus_that:$mey;
		//判断这条订单是否封顶,如果剩下的>要发的，取要发的，否则取剩下的
		$bonus_minu = ($all_mey-$bonus_shaoshang);
		$bonus_fengding  = $bonus_minu>0?$bonus_shaoshang:$all_mey;
		if($bonus_fengding>0){
			//给这个人发加速奖
			$beizhu = "获得第:2代会员" . $vo1['mobile'] . "分享算力:" . $get_pre_b1 . " x " . $real_levl;
			/*给v会员加速 */
			$money_sql="update clt_users set money_benjin=money_benjin+".$bonus_fengding."  where id=".$v['id'];
			querys($money_sql);
			/*增加记录*/
			$money_benjin1=getuser_money_benjin($v['id']);
			$sql_money_log = "insert into clt_money_log values (NULL,'".time()."','+".$bonus_fengding."',3,'".$v['id']."','".$money_benjin1."','".$beizhu."',12,0,0,0)";
			querys($sql_money_log);
			edit_add_bonus($v['id'],$bonus_fengding);
			if ($all_mey - $bonus_fengding <= 0) {
				$all_mey = 0;
				break;
			} else {
				$all_mey = $all_mey - $bonus_fengding;
			}
		}
	}
}
if($v['recount']>=5&&$all_mey>0){

	//先获取本人的比例3代  $real_lev
	$real_levl2 = $levels['b7']/100;
	$relevel2 = $v['relevel'] + 3;
	$sql_pre_user2 = "select id,mobile from clt_users where rpath like '%".$new_path."%' and relevel=".$relevel2."  and activation=1";
	$pre_userlist2 = getall($sql_pre_user2);
	foreach($pre_userlist2 as $ke=>$vo2){
		//查这个下级会员的bonus表里面的b1值
		$pre_b1 = getone("select b1 from clt_bonus where uid='".$vo2['id']."' and createtime>'".$today_time."'");
		$get_pre_b1 = $pre_b1['b1'];
		$mey=$get_pre_b1*$real_levl2;
		//判断本人烧伤,下级会员的b1和本人b1,选择小的那个
		$bonus_shaoshang = $bonus_that<$mey?$bonus_that:$mey;
		//判断这条订单是否封顶,如果剩下的>要发的，取要发的，否则取剩下的
		$bonus_minu = ($all_mey-$bonus_shaoshang);
		$bonus_fengding  = $bonus_minu>0?$bonus_shaoshang:$all_mey;
		if($bonus_fengding>0){
			//给这个人发加速奖
			$beizhu = "获得第:3代会员" . $vo2['mobile'] . "分享算力:" . $get_pre_b1 . " x " . $real_levl2;
			/*给v会员加速 */
			$money_sql="update clt_users set money_benjin=money_benjin+".$bonus_fengding."  where id=".$v['id'];
			querys($money_sql);
			/*增加记录*/
			$money_benjin1=getuser_money_benjin($v['id']);
			$sql_money_log = "insert into clt_money_log values (NULL,'".time()."','+".$bonus_fengding."',3,'".$v['id']."','".$money_benjin1."','".$beizhu."',12,0,0,0)";
			querys($sql_money_log);
			edit_add_bonus($v['id'],$bonus_fengding);
			if ($all_mey - $bonus_fengding <= 0) {
				$all_mey = 0;
				break;
			} else {
				$all_mey = $all_mey - $bonus_fengding;
			}
		}
	}
}

	/*//先判断这个人的rcount>=3 并且 $all_mey
	if($v['recount']<5 || empty($v['recount']) ||  empty($all_mey) || $all_mey<=0){
		//跳出本次循环
		set_deal($v['id']);continue;
	}*/



/*
	//先判断这个人的rcount>=7 并且 $all_mey
	if($v['recount']<7 || empty($v['recount']) ||  empty($all_mey) || $all_mey<=0){
		//跳出本次循环
		set_deal($v['id']);continue;
	}*/
	if($v['recount']>=7&&$all_mey>0){
		//先获取本人的比例3代  $real_lev
		$real_levl3 = $levels['b8']/100;
		$relevel3 = $v['relevel'] + 4;
		$sql_pre_user3 = "select id,mobile from clt_users where rpath like '%".$new_path."%' and relevel=".$relevel3."  and activation=1";
		$pre_userlist3 = getall($sql_pre_user3);
		foreach($pre_userlist3 as $ke=>$vo3){
			//查这个下级会员的bonus表里面的b1值
			$pre_b1 = getone("select b1 from clt_bonus where uid='".$vo3['id']."' and createtime>'".$today_time."'");
			$get_pre_b1 = $pre_b1['b1'];
			$mey=$get_pre_b1*$real_levl3;
			//判断本人烧伤,下级会员的b1和本人b1,选择小的那个
			$bonus_shaoshang = $bonus_that<$mey?$bonus_that:$mey;
			//判断这条订单是否封顶,如果剩下的>要发的，取要发的，否则取剩下的
			$bonus_minu = ($all_mey-$bonus_shaoshang);
			$bonus_fengding  = $bonus_minu>0?$bonus_shaoshang:$all_mey;
			if($bonus_fengding>0){
				//给这个人发加速奖
				$beizhu = "获得第:4代会员" . $vo3['mobile'] . "分享算力:" . $get_pre_b1 . " x " . $real_levl3;
				/*给v会员加速 */
				$money_sql="update clt_users set money_benjin=money_benjin+".$bonus_fengding."  where id=".$v['id'];
				querys($money_sql);
				/*增加记录*/
				$money_benjin1=getuser_money_benjin($v['id']);
				$sql_money_log = "insert into clt_money_log values (NULL,'".time()."','+".$bonus_fengding."',3,'".$v['id']."','".$money_benjin1."','".$beizhu."',12,0,0,0)";
				querys($sql_money_log);
				edit_add_bonus($v['id'],$bonus_fengding);
				if ($all_mey - $bonus_fengding <= 0) {
					$all_mey = 0;
					break;
				} else {
					$all_mey = $all_mey - $bonus_fengding;
				}
			}
		}
	}



	/*//先判断这个人的rcount>=7 并且 $all_mey
	if($v['recount']<10 || empty($v['recount']) ||  empty($all_mey) || $all_mey<=0){
		//跳出本次循环
		set_deal($v['id']);continue;
	}*/
	if($v['recount']>=10&&$all_mey>0){
//先获取本人的比例3代  $real_lev
		$real_levl4 = $levels['b9']/100;
		$relevel4 = $v['relevel'] + 5;
		$relevel5 = $v['relevel'] + 10;
		$sql_pre_user4 = "select id,mobile from clt_users where rpath like '%".$new_path."%' and relevel BETWEEN  ".$relevel4." and ".$relevel5."  and activation=1";
		$pre_userlist4 = getall($sql_pre_user4);
		foreach($pre_userlist4 as $ke=>$vo3){
			//查这个下级会员的bonus表里面的b1值
			$pre_b1 = getone("select b1 from clt_bonus where uid='".$vo3['id']."' and createtime>'".$today_time."'");
			$get_pre_b1 = $pre_b1['b1'];
			$mey=$get_pre_b1*$real_levl4;
			//判断本人烧伤,下级会员的b1和本人b1,选择小的那个
			$bonus_shaoshang = $bonus_that<$mey?$bonus_that:$mey;
			//判断这条订单是否封顶,如果剩下的>要发的，取要发的，否则取剩下的
			$bonus_minu = ($all_mey-$bonus_shaoshang);
			$bonus_fengding  = $bonus_minu>0?$bonus_shaoshang:$all_mey;
			if($bonus_fengding>0){
				//给这个人发加速奖
				$beizhu = "获得第:5代会员" . $vo3['mobile'] . "分享算力:" . $get_pre_b1 . " x " . $real_levl4;
				/*给v会员加速 */
				$money_sql="update clt_users set money_benjin=money_benjin+".$bonus_fengding."  where id=".$v['id'];
				querys($money_sql);
				/*增加记录*/
				$money_benjin1=getuser_money_benjin($v['id']);
				$sql_money_log = "insert into clt_money_log values (NULL,'".time()."','+".$bonus_fengding."',3,'".$v['id']."','".$money_benjin1."','".$beizhu."',12,0,0,0)";
				querys($sql_money_log);
				edit_add_bonus($v['id'],$bonus_fengding);
				if ($all_mey - $bonus_fengding <= 0) {
					$all_mey = 0;
					break;
				} else {
					$all_mey = $all_mey - $bonus_fengding;
				}
			}
		}
	}

	/*更新理财订单状态*/
	$order_liset=getall("select id,summey,mey from clt_licai_order where uid=".$v['id']." and summey>0 order by createtime asc");
	$sql_bonus_v = "select b2 from clt_bonus where uid='".$v['id']."' and createtime>'".$today_time."'";
	$vb2=getone($sql_bonus_v);
	$b2=$vb2['b2'];
	if($b2>0){
		foreach ($order_liset as $ke=>$va){
			if ($va['summey'] - $b2 > 0) {
				$data_summey = $va['summey'] - $b2;
				$b2 = 0;
				$start=1;
			} else {
				$b2 = $b2 - $va['summey'];
				$beizhuchuju = "会员动态奖出局转出金额" . $va['mey'];
				//mey_log(3, $v['id'], 2, 18, $va['mey'], $va['mey'], $beizhuchuju);
				$sql_money_log = "insert into clt_money_log values (NULL,'".time()."','-". $va['mey']."',3,'".$v['id']."','".$va['mey']."','".$beizhuchuju."',18,0,0,0)";
				querys($sql_money_log);
				$data_summey = 0;
				$start = 2;
			}
			$sql_licai_log="update clt_licai_order set summey=".$data_summey.",start=".$start." where id=".$va['id'];
			querys($sql_licai_log);
			//db('licai_order')->where('id', $va['id'])->update($data);
			if ($b2 <= 0) {
				break;
			}
		}
		$beizhu4 = "链接算力钱包释放到流通钱包,释放金额:" .$vb2['b2'];
//		mey_log(3, $value['id'], 2, 13, $bonus11['b2'], $value['money_benjin'] + $bonus11['b2'], $beizhu4);
		$sql_money_log = "insert into clt_money_log values (NULL,'".time()."','-". $vb2['b2']."',3,'".$v['id']."','".($v['mey']+$vb2['b2'])."','".$beizhu4."',13,0,0,0)";
		querys($sql_money_log);
	}

	/*把处理过的数据改为已处理*/
	set_deal($v['id']);continue;
	
}
?>



